package com.bite.Queue;

import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class LoopQueue {
    //队头
    private int head;
    //队尾
    private int rear;
    //数组实现
    private int[] queue;
    //有效个数
    private int usedSize;
    //构造方法
    LoopQueue(int value) {
        queue = new int[value];
    }
    //插入数据
    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        queue[rear] = value;
        rear = (rear + 1) % queue.length;
        usedSize++;
        return true;
    }
    //取出数据
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        head = (head + 1) % queue.length;
        usedSize--;
        return true;
    }
    //是否为空
    public boolean isEmpty() {
        return usedSize == 0;
    }
    //是否满了
    public boolean isFull() {
        return usedSize == queue.length;
    }
    //获取头部元素
    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return queue[head];
    }
    //获取尾部元素
    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        int ret = rear == 0 ? queue.length : rear - 1;
        return queue[ret];
    }
}
